package Person.exa.impl;

import Person.exa.Dao.StudnetManagerDao;
import Person.exa.Student.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManagerImpl implements StudnetManagerDao {
    static Scanner sc = new Scanner(System.in);
    @Override
    public void show(ArrayList<Student> list) {

    }

    @Override
    public void add(ArrayList<Student> list) {

        System.out.println("请输入学生学号： ");
        String sid = sc.nextLine();
        System.out.println("请输入学生姓名： ");
        String name = sc.nextLine();
        System.out.println("请输入学生年龄： ");
        String age = sc.nextLine();
        System.out.println("请输入学生居住地： ");
        String address = sc.nextLine();
        Student studnet = new Student(name,age,sid,address);
        list.add(studnet);
        System.out.println("添加成功");
    }

    @Override
    public void delete(ArrayList<Student> list) {
if(isEmpty(list)){
    System.out.println("无信息，请先添加后在进行查询： ");
    return;
}
String sid;
while(true){
    System.out.println("请输入需要删除的学生学号： ");
    sid = sc.nextLine();
    if(!isExist(list,sid)){
        System.out.println("学号错误，请重新输入： ");
    }else{
        break;
    }
}
for(Student s:list){
    if(s.getSid().equals(sid)){
        list.remove(s);
        System.out.println("删除成功！ ");
        return;
    }
}

    }
    public void update(ArrayList<Student> list) {

    }

    @Override
    public boolean isEmpty(ArrayList<Student> list) {
     if(list.size()==0){
         return true;
     }
     return false;
    }

    @Override
    public boolean isExist(ArrayList<Student> list, String sid) {
        for(Student s:list){
            if(s.getSid().equals(sid)){
                return true;
            }
        }
        return false;
    }
}
